package com.offcn.utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;

/**
 * 用于上传文件的工具类
 */
public class FileUploadUtils {
    public static String upload(HttpServletRequest req) throws IOException, ServletException {
        Part myFile = req.getPart("myFile");
        String fileName = myFile.getSubmittedFileName();    //1234.jpg
        fileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        //保存的位置
        //String path="C:\\Users\\Administrator\\Desktop\\upload\\";
        //File file = new File(path);

        //上传到linux
        File file = new File("/upload");
        if (!file.exists()){
            file.mkdirs();
        }
        File file2 = new File(file,fileName);
        myFile.write(file2.getPath());
        return fileName;

    }
}
